package com.feihao.zhou.Test;

import com.feihao.zhou.bean.*;
import com.feihao.zhou.mapper.ManyToMany;
import com.feihao.zhou.mapper.OneToMany;
import com.feihao.zhou.mapper.OneToOneMapper;
import com.feihao.zhou.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test {

    //单表查询所有学生：
    @org.junit.Test
    public void test01() {
        SqlSession sqlSession = DBUtils.getSqlSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<Student> list = mapper.selectAllStudent();
//        System.out.println(list.size());
        list.forEach((s) -> {
            System.out.println(s);
        });
    }

    //单表查询指定id学生：

    @org.junit.Test
    public void test02() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectStudentById(2);
        System.out.println(student);
    }

    //单表新增学生
    @org.junit.Test
    public void test03() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student(null, "小磊磊", 23, 2);
        Integer integer = mapper.insertStudent(student);
        //提交事务
        sqlSession.commit();
        System.out.println("是否成功：" + integer);
    }

    //单表修改学生：
    @org.junit.Test
    public void test04() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer integer = mapper.updateStudent("里斯", 88, 2);
        //提交事务：
        sqlSession.commit();
        System.out.println(integer);
    }

    //单表删除学生：
    @org.junit.Test
    public void test05() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer integer = mapper.deleteStudentById(12);
        sqlSession.commit();
        System.out.println("删除成功：" + integer);
    }

    /*----------------------------------多表查询：------------------------------------------------*/
    //一对一：card  对应 person
    @org.junit.Test
    public void test001() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
        List<Card> cards = mapper.selectAllCard();
        for (Card card : cards) {
            System.out.println(card.getId() + "------" + card.getNumber());
            System.out.println("\t" + card.getPerson());
        }
    }

    //一对多：一个classes 对应 对个学生
    @org.junit.Test
    public void test002() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        OneToMany mapper = sqlSession.getMapper(OneToMany.class);
        List<Classes> classes = mapper.selectAllClasses();
        for (Classes aClass : classes) {
            System.out.println(aClass.getId() + "," + aClass.getName());
            List<Student> students = aClass.getStudents();
            System.out.println("\t" + students);
        }
    }


    //都对多： 一个学生有多门课程  ，一门课程可以对应多个学生
    @org.junit.Test
    public void test003() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        ManyToMany mapper = sqlSession.getMapper(ManyToMany.class);
        List<Student_Course> student_courses = mapper.selectAllStudentCourse();
        for (Student_Course student_cours : student_courses) {
            System.out.println(student_cours.getId() + "," + student_cours.getName() + "," + student_cours.getAge() + "," + student_cours.getCid());
            List<Course> course = student_cours.getCourse();
            for (Course course1 : course) {
                System.out.println("\t" + course1);
            }
        }
    }

}
